﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Provider;
using System.Data;

namespace AccessToDataEnergyLibrury
{
    public partial class AccessDataEnergyLibrury
    {
        public List<Employee> GetAllEmployee()
        {
            DataRowCollection searchedRow = this.energyDataSet.employee.Rows;

            return this.GetRestPartsOfEmployee(searchedRow);
        }

        private List<Employee> GetRestPartsOfEmployee(DataRowCollection searchedRows)
        {
            if (searchedRows == null || searchedRows.Count == 0)
            {
                return null;
            }
            else
            {
                List<Employee> employees = new List<Employee>();

                foreach(energyzmemDataSet.employeeRow employeeRow  in searchedRows)
                {
                    Employee employee = new Employee();
                    employee.CodeEmployee = employeeRow.codeEmployee.ToString();
                    employee.Number = employeeRow.pers_num.ToString();
                    employee.LastName = employeeRow.last_name;
                    employee.Name = employeeRow.name;
                    employee.Patronymic = employeeRow.patronymic;
                    employee.BirthDate = employeeRow.birth_date.ToString();
                    employee.MobNumberPhone = employeeRow.Field<string>("numder_mobile_phones");
                    employee.Gender = employeeRow.gender;
                    employee.CategoryAppointment = employeeRow.Field<int>("FK_categoriAppointment").ToString();

                    if (employeeRow.addressRow != null)
                    {
                        employee.Adress = employeeRow.addressRow.region.ToString();
                    }
                    else
                    {
                        throw new Exception("Нет данных в таблице Adress для співробітників");
                    }
                    
                        if(employeeRow.departmentsRow != null)
                        {
                            employee.Appointment = employeeRow.departmentsRow.departamets_abbreviation.ToString();
                        }
                        else
                        {
                            throw new Exception("Нет данных в таблице Departments для співробітників");
                        }
                   employees.Add(employee);
                }
                return employees;
            }
        }

    }
}
